<template>
  <div>
    <!-- 卡片 -->
    <div>
      <el-card class="box-card">
        <div slot="header">
          <span v-if="!productId" class="addTitle">新增商品</span>
          <span v-else class="addTitle">编辑商品</span>
          <el-button v-if="active" class="btnList" @click="back">取消</el-button>
          <el-button v-if="active" type="primary" class="btnList" @click="save">保存</el-button>
        </div>

      </el-card>
      <!-- 商品 -->
      <div class="addCom common">
        <div v-if="active" class="leftCom">
          <el-form ref="form"  :rules="rules" :model="form" label-width="80px" style="padding: 40px 40px;">
            <el-form-item label="商品名称" prop="productName" >
              <el-input v-model="form.productName"  />
            </el-form-item>
            <el-form-item label="规格描述" prop="desc">
              <el-input v-model="form.desc" />
            </el-form-item>
            <el-form-item label="所需积分" prop="points">
              <el-input v-model="form.points" oninput ="value = value.replace(/[^0-9.]/g,'')" />
            </el-form-item>
            <el-form-item label="封面图" >
              <div class="upload-wrap">
                <el-upload
                  class="avatar-uploader"
                  :action="action"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess"
                  :before-upload="beforeAvatarUpload"
                >
                  <img v-if="form.image" :src="form.image" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon" />
                </el-upload>
              </div>
              <p>
                <span style="color: blue">参考尺寸:</span>
                <span style="color: red">1000*1000</span>
              </p>
            </el-form-item>
            <el-form-item label="详情图">
              <!--              <el-input v-model="form.productImage" />-->
              <el-upload
                :action="action"
                list-type="picture-card"
                :on-preview="handlePictureCardPreview"
                :on-success="handlePdImageSuccess"
                :before-upload="beforeAvatarUpload"
                :on-remove="handleRemove"
                :file-list="fileList"
              >
                <i class="el-icon-plus" />
              </el-upload>
              <el-dialog :visible.sync="dialogVisible">
                <img width="100%" :src="dialogImageUrl" alt="">
              </el-dialog>
              <p>
                <span style="color: blue">参考尺寸:</span>
                <span style="color: red">790*1357</span>
              </p>
            </el-form-item>
            <el-form-item label="参考价" prop="price"  >
              <el-input v-model="form.price" oninput="value=value.replace(/[^\d.]/g,'')"/>
            </el-form-item>
            <el-form-item label="库存" prop="stock">
              <el-input v-model="form.stock" oninput ="value = value.replace(/[^0-9.]/g,'')" />
            </el-form-item>
            <el-form-item label="上架状态">
              <el-radio-group v-model="form.isShelve">
                <el-radio :label="1">上架</el-radio>
                <el-radio :label="0">下架</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// import Tinymce from '@/components/Tinymce'
import {
  getById,
  save
} from '@/api/pointCommodity'
import { uploadUrl } from '@/utils/request'
export default {
  name: '',
  components: {
  },
  data() {
    return {
      active: 1,
      action: uploadUrl,
      form: {
        image: '',
        isShelve: 1
      },
      headers: {
        Authorization: ''
      },
      dataObj: {
        folderId: 1
      },
      rules: {
        productName: [
          { required: true, message: '请输入产品名！', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '请输入规格描述！', trigger: 'blur' }
        ],
        points: [
          { required: true, message: '请输入所需积分！', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '请输入参考价格！', trigger: 'blur' }
        ],
        stock: [
          { required: true, message: '请输入所需库存！', trigger: 'blur' }
        ]
      },
      fileList: [],
      dialogImageUrl: [],
      dialogVisible: false
    }
  },
  computed: {
    productId() {
      return this.$route.query.productId
    }
  },
  created() {
    this.headers.tenant = 'MDAwMA=='
  },
  mounted() {
    console.log(this.productId)
    if (this.productId) {
      this.details()
    }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.form.image = res.data.url
    },
    beforeAvatarUpload(file) {
      const isLt10M = file.size / 1024 / 1024 < 2
      if (!isLt10M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isLt10M
    },
    handlePdImageSuccess(res, file, fileList) {
      console.log(res.data.url)
      // this.fileList = fileList
      this.fileList.push({
        url: res.data.url
      })
    },
    // 移除图片
    handleRemove(file, fileList) {
      console.log(file, fileList)
      // this.fileList = fileList
      const { url } = file
      this.fileList = this.fileList.filter(item => {
        return item.url !== url
      })
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      console.log(file)
      this.dialogVisible = true
    },
    // 返回
    back() {
      this.$router.go(-1)
    },
    // 保存
    async save() {
      if (!this.form.productName) {
        this.$message({
          type: 'error',
          message: '请输入商品名称！'
        })
        return false
      }

      if (!this.form.desc) {
        this.$message({
          type: 'error',
          message: '请输入卖点简介！'
        })
        return false
      }

      if (!this.form.points) {
        this.$message({
          type: 'error',
          message: '请输入所需积分！'
        })
        return false
      }

      if (!this.form.image) {
        this.$message({
          type: 'error',
          message: '请选择封面图！'
        })
        return false
      }

      // 详情图
      var pdImg = ''
      console.log(this.fileList)
      this.fileList.forEach((item) => {
        var url = item.url
        pdImg = pdImg + url + ','
      })
      this.form.productImage = pdImg
      if (!this.form.productImage) {
        this.$message({
          type: 'error',
          message: '请选择商品详情图！'
        })
        return false
      }

      if (!this.form.price) {
        this.$message({
          type: 'error',
          message: '请输入参考价！'
        })
        return false
      }

      if (!this.form.stock) {
        this.$message({
          type: 'error',
          message: '请输入库存！'
        })
        return false
      }

      console.log(pdImg)
      if (this.productId) {
        this.form.productId = this.productId
        this.form.type = 2// 修改
      } else {
        this.form.type = 1 // 新增
      }
      console.log(this.form)
      const res = await save(this.form)
      console.log(res)
      if (res.code === '200') {
        this.$message({
          type: 'success',
          message: '成功!'
        })
        this.$router.push({
          name: 'index'
        })
      } else {
        this.$message({
          message: '保存失败！',
          type: 'error'
        })
      }
    },
    // 获取详情
    async details() {
      const res = await getById({ productId: this.productId })
      console.log(res)
      this.form = res.data
      let productImage = res.data.productImage
      productImage = productImage.substring(0, productImage.lastIndexOf(','))
      const split = productImage.split(',')
      this.fileList = split.map(item => {
        console.log(item)
        return {
          name: item,
          url: item
        }
      })
    }
  }

}
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
<style scoped lang='scss'>
@import url("../../styles/elDialog.scss");
.btnList {
  float: right;
  padding: 3px 0;
  width: 100px;
  height: 48px;
  border-radius: 4px;
  margin-right: 30px;
}
.addTitle {
  font-size: 24px;
  color: #333333;
  line-height: 50px;
}
.common {
  display: flex;
  justify-content: center;
  align-items: center;
}
.one_class {
  width: 40px;
  height: 40px;
  background: #3a68f2;
  border-radius: 50%;
  color: #ffffff;
  font-size: 24px;
  margin: 0 10px;
}
.two_class {
  color: #3a68f2;
  font-size: 24px;
}
.line {
  width: 230px;
  height: 2px;
  background: #e0e5eb;
  margin: 0 20px;
}
.t_class {
  width: 40px;
  height: 40px;
  background: #dddddd;
  border-radius: 50%;
  font-size: 24px;
  color: #333333;
  margin: 0 10px;
}
.w_class {
  font-size: 24px;
  color: #666666;
}
.addCom {
  margin: 10px;
  justify-content: space-around;
  align-items: unset;
  .leftCom {
    width: 68%;
    background: #ffffff;
  }
  .rightCom {
    width: 28%;
    background: #ffffff;
  }
}
.centerCom {
  width: 1660px;
  background: #ffffff;
  box-shadow: 0px 5px 20px 0px rgba(51, 51, 51, 0.15);
  border-radius: 4px;
}
</style>
